import Vue from 'vue'
import Vuex from 'vuex'
import Layout from "../layout/index.vue"
Vue.use(Vuex)
//深度筛选
function filterRouter(value, field) {
    if (!(value instanceof Array)) return;
    return value.filter(item => {
        if (item.children) {
            console.log(item.children)
            item.children=filterRouter(item.children,field)
        }
        return item.meta.permission[field]
    })
};
//
export default new Vuex.Store({
    state: {
        num: 2,
        apple: "苹果",
        price: 999,
        stateRouter: [
            {
                path: "/home",
                redirect: "/home/nav",
                component: Layout,
                meta: {
                    title: "主页", Icon: "layui-icon layui-icon-login-wechat", permission: {
                        common: true, Administrator: true, manager: true
                    }
                },
                children: [
                    {
                        path: "/home/details",
                        name: "details",
                        component: () => import("../views/details.vue"),
                        meta: {
                            title: "使用须知", Icon: "layui-icon layui-icon-login-qq"
                            , permission: {
                                common: false, Administrator: true, manager: true
                            }
                        }

                    },
                    {
                        path: "/home/nav",
                        name: "Nav",
                        component: () => import("../views/nav.vue"),
                        meta: {
                            title: "首页", Icon: "layui-icon layui-icon-login-qq"
                            , permission: {
                                common: false, Administrator: false, manager: true
                            }
                        }

                    }, {
                        path: "/home/tt",
                        name: "tt",
                        component: () => import("../views/about.vue"),
                        meta: { title: "关于", Icon: "layui-icon layui-icon-login-qq" 
                        , permission: {
                            common: false, Administrator: false, manager: true
                        }}
                        

                    }, {
                        path: "/home/fwb",
                        name: "fwb",
                        component: () => import("../views/wangeditor.vue"),
                        meta: {
                            title: "富文本", Icon: "layui-icon layui-icon-login-qq"
                            , permission: {
                                common: true, Administrator: false, manager: true
                            }
                        }

                    },
                    {
                        path: "/home/echarts",
                        name: "echarts",
                        component: () => import("../views/echarts.vue"),
                        meta: {
                            title: "图表展示", Icon: "layui-icon layui-icon-login-qq"
                            , permission: {
                                common: true, Administrator: false, manager: true
                            }
                        }

                    },
                ]
            },

            {
                path: "/about",
                name: "About",
                redirect: "/about/nav",

                component: Layout,
                meta: {
                    title: "账号注册", Icon: "layui-icon layui-icon-login-weibo", permission: {
                        common: true, Administrator: false, manager: true
                    }
                },
                children: [
                    {
                        path: "/about/nav",
                        name: "Nav",
                        component: () => import("../views/nav.vue"),
                        meta: {
                            title: "素材", Icon: "layui-icon layui-icon-login-qq", permission: {
                                common: true, Administrator: true, manager: true
                            }
                        }

                    }, {
                        path: "/abou/ttt",
                        name: "tt",
                        component: () => import("../views/about.vue"),
                        meta: {
                            title: "其他",
                            Icon: "layui-icon layui-icon-login-qq",
                            permission: {
                                common: true, Administrator: true, manager: true
                            }
                        }

                    },
                ]

            },
        ]

    },
    mutations: {
        dynamicRouter(state, field) {
            if (field == "common") {
                state.stateRouter = filterRouter(state.stateRouter, field)
            } else if (field == "Administrator") {
                state.stateRouter = filterRouter(state.stateRouter, field)
            } else if (field == "manager") {
                state.stateRouter = state.stateRouter.filter(item => item.meta.permission.manager)
            }
        }
    },
    actions: {

    },
    getters: {

    },
    modules: {
    }
})
